package org.ethan.lyl;


import br.com.objectos.core.lang.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @Author:Eason Lau
 * @Date:2018/10/29
 * @Description: 主程序
 */
public class Main {
    static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {

        String adbDir = Screenshot.class.getResource("/").getPath().substring(1)+"adb/";

        logger.info("=============程序开始=============");
        try {
            for (int p = 1; p <= 1; p++) {
                logger.info("=============第{}次=============",p);
                logger.info("=============开始截屏=============");
                String screenshotPath = Screenshot.screenshotByADB();
                Preconditions.checkArgument(screenshotPath != null, "截屏失败");
                logger.info("=============分析二维数组=============");
                ImgAnalysis.Coordinate[][] array = ImgAnalysis.getArray(screenshotPath);
                Preconditions.checkArgument(array != null, "数组分析失败");
                logger.info("=============计算路径=============");
                List<PathAnalysis.ArrayIndex> pathArray = PathAnalysis.getPath(array);
                logger.info("=============开始画线=============");
                Runtime rt = Runtime.getRuntime();
                for (int i = 0; i < pathArray.size() - 1; i++) {
                    logger.info("=======第{}点=======", i + 1);
                    String cmd = String.format("cmd.exe /c "+adbDir+"adb shell input swipe %d %d %d %d",
                            array[pathArray.get(i).i][pathArray.get(i).j].Y,
                            array[pathArray.get(i).i][pathArray.get(i).j].X,
                            array[pathArray.get(i + 1).i][pathArray.get(i + 1).j].Y,
                            array[pathArray.get(i + 1).i][pathArray.get(i + 1).j].X
                    );
                    Process process = rt.exec(cmd);
                    //process.waitFor();
                    Thread.sleep(500);
                }
                rt.freeMemory();
                Thread.sleep(2000);
                /*Process process = rt.exec("cmd.exe /c D:\\OneDrive\\DEV\\Tools\\adb\\adb shell input tap 925 661");
                process.waitFor();
                Thread.sleep(1500);
                process = rt.exec("cmd.exe /c D:\\OneDrive\\DEV\\Tools\\adb\\adb shell input tap 555 1609");
                process.waitFor();
                Thread.sleep(1500);*/


            }

            /*Runtime rt = Runtime.getRuntime();
            String cmd = String.format("cmd.exe /c D:\\OneDrive\\DEV\\Tools\\adb\\adb shell input swipe 608 547 608 424");
            System.out.println(rt.exec(cmd));*/

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("============={}=============", e.getMessage());
        }


    }
}
